import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Index from '../views/Index'
import Login from '../views/Login'

Vue.use(Router)

export const appRoute = {
  path: '/',
  name: 'Index',
  text: '首页',
  icon: '',
  iconAlt: '',
  component: Index,
  meta: {
    requireAuth: true
  },
  children: [
    {
      path: '/soccer',
      name: 'soccer',
      text: '足球关键字',
      icon: 'keyboard_arrow_up',
      iconAlt: 'keyboard_arrow_down',
      component: HelloWorld,
      meta: {
        requireAuth: true
      },
      children: [
        {
          path: '/basketball',
          name: 'basketball',
          text: '篮球关键字',
          icon: 'contacts',
          iconAlt: '',
          component: HelloWorld,
          meta: {
            requireAuth: true
          }
        }
      ]
    },
    {
      path: '/basketball',
      name: 'basketball',
      text: '篮球关键字',
      icon: 'history',
      iconAlt: '',
      component: HelloWorld,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/common',
      name: 'common',
      text: '通用关键字',
      icon: 'phonelink',
      iconAlt: '',
      component: HelloWorld,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/player',
      name: 'player',
      text: '球员关键字',
      icon: 'content_copy',
      iconAlt: '',
      component: HelloWorld,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/team',
      name: 'team',
      text: '球队关键字',
      icon: 'settings',
      iconAlt: '',
      component: HelloWorld,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/venue',
      name: 'venue',
      text: '球场关键字',
      icon: 'help',
      iconAlt: '',
      component: HelloWorld,
      meta: {
        requireAuth: true
      }
    }
  ]
}

let _router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: {
        hidden: true
      }
    },
    appRoute,
    {path: '*', redirect: '/'}
  ]
})

_router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requireAuth)) {
    // this route requires auth, check if logged in
    // if not, redirect to login page.
    let token = sessionStorage.getItem('app-token')
    // return next()
    console.log('user token : ', token)
    if (!token) {
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      })
    } else {
      next()
    }
  } else {
    next() // 确保一定要调用 next()
  }
})
export default _router
